#!/bin/sh 
#-x

which flash_eraseall nandwrite
#which ls
case $? in
    0 )
    echo " The required executables are installed. Proceeding. "
    ;;
    * )
    echo " The required executables are not installed. Exit. "
    exit 1 
    ;;
esac

if [ "$1" = "-h" ]
then #if the argument is -h 
    echo " Usage $0 -h|image.splash " #echo the usage msg. 
    echo "       -h - Displays this help message." 
elif [ $# -ne 1 ] 
then
    echo " You inserted the wrong number of arguments: $# " 
    echo " Usage $0 -h|image.splash " #echo the usage msg. 
    exit 1
elif [ ! -f $1 ]
then
    echo " The file $1 doesn't exist. Exiting. " 
    exit 1
else 
    echo " Some informations: "
    cat /proc/mtd

    echo " And some back-ups: "
    mkdir flashimage_backup
    cat /dev/mtd4 > flashimage_backup/splash.`date "+%Y-%m-%d"`.image

    echo " You are going to flash $1 on /dev/mtd4. Is that OK? [y/n] "
    read ceck
    case $ceck in 
        y )
        echo " Very well... "
        flash_eraseall /dev/mtd4
        nandwrite -p /dev/mtd4 $1
        echo " All done... bye bye... "
        ;;
        * )
        echo " Nothing more to do, bye bye :-) "
        ;;
    esac

fi
